package com.aliyun.messagemgt.application.service.Impl;

import com.aliyun.messagemgt.common.annotation.PagingAnnotation;
import com.aliyun.messagemgt.application.service.MsgSourceService;
import com.aliyun.messagemgt.application.service.OperateLogService;
import com.aliyun.messagemgt.dto.OperateLogExportModel;
import com.aliyun.messagemgt.dto.query.OperateLogQuery;
import com.aliyun.messagemgt.dto.vo.DepVo;
import com.aliyun.messagemgt.dto.vo.OperateResourceTypeVO;
import com.aliyun.messagemgt.domain.repository.MessageTemplateRepository;
import com.aliyun.messagemgt.domain.repository.OperateLogRepository;
import com.aliyun.messagemgt.dto.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description
 * @Author hzw
 * @Date 2020/12/17 11:19
 * @Version 1.0
 */
@Service
public class OperateLogServiceImpl implements OperateLogService {

    @Autowired
    private OperateLogRepository operateLogRepository;

    @Autowired
    private MsgSourceService msgSourceService;

    @Autowired
    private MessageTemplateRepository messageTemplateRepository;

    private static Logger logger = LoggerFactory.getLogger(OperateLogServiceImpl.class);

    @Override
    @PagingAnnotation
    public PageInfo listOperateLog(OperateLogQuery operateLogQuery) {
        PageHelper.startPage(operateLogQuery.getPageNum(), operateLogQuery.getPageSize());
        List<OperateLogExportModel> operateLogExportModelVOS = operateLogRepository.listOperateLog(operateLogQuery);
        PageInfo objectPageInfo = new PageInfo<>(operateLogExportModelVOS);
        for (OperateLogExportModel model : operateLogExportModelVOS) {
            switch (model.getOperateResourceType()) {
                // 操作类目转换中文
                case 1:
                    model.setOperateResourceTypeName("数据源");
                    break;
                case 2:
                    model.setOperateResourceTypeName("场景");
                    break;
                case 3:
                    model.setOperateResourceTypeName("计划");
                    break;
                case 4:
                    model.setOperateResourceTypeName("消息档案");
                    break;
                case 5:
                    model.setOperateResourceTypeName("消息源");
                    break;
                case 6:
                    model.setOperateResourceTypeName("敏感日志");
                    break;
                case 7:
                    model.setOperateResourceTypeName("操作日志");
                    break;
                case 8:
                    model.setOperateResourceTypeName("待办事项");
                    break;
                case 9:
                    model.setOperateResourceTypeName("渠道管理");
                    break;
                case 10:
                    model.setOperateResourceTypeName("监控管理");
                    break;
                case 11:
                    model.setOperateResourceTypeName("渠道号");
                    break;
                case 12:
                    model.setOperateResourceTypeName("日志下载");
                    break;
                case 13:
                    model.setOperateResourceTypeName("应用管理");
                    break;
                case 14:
                    model.setOperateResourceTypeName("模板管理");
                    break;
                case 15:
                    model.setOperateResourceTypeName("短信配额");
                    break;
                default:
                    break;
            }

            // operate_type中文转换
            switch (model.getOperateType()) {
                // 操作类目转换中文
                case 1:
                    model.setOperateTypeName("新增");
                    break;
                case 2:
                    model.setOperateTypeName("提交");
                    break;
                case 3:
                    model.setOperateTypeName("修改");
                    break;
                case 4:
                    model.setOperateTypeName("删除");
                    break;
                case 5:
                    model.setOperateTypeName("导出");
                    break;
                case 6:
                    model.setOperateTypeName("批准");
                    break;
                case 7:
                    model.setOperateTypeName("审批不通过");
                    break;
                case 8:
                    model.setOperateTypeName("导入");
                    break;
                case 9:
                    model.setOperateTypeName("下线");
                    break;
                case 10:
                    model.setOperateTypeName("禁用");
                    break;
                case 11:
                    model.setOperateTypeName("启用");
                    break;
                default:
                    break;
            }

            // 拼接查询详情
            StringBuilder sb = new StringBuilder();
            sb.append(model.getOperateUserName() != null ? model.getOperateUserName() + " " : "");
            sb.append(model.getOperateTypeName() != null ? model.getOperateTypeName() + " " : "");
            sb.append(model.getOperateResourceTypeName() != null ? model.getOperateResourceTypeName() + " " : "");
            sb.append("操作内容:[");
            sb.append(model.getOperateResourceName() != null ? model.getOperateResourceName() + "] " : "] ");
            sb.append("操作结果:[");
            sb.append(model.getOperateStatus() == 1 ? "成功]" : "失败]");
            model.setDetails(sb.toString());
        }
        objectPageInfo.setList(operateLogExportModelVOS);
        return objectPageInfo;
    }

    @Override
    public List<DepVo> operateLogListOwnerDept(OperateLogQuery operateLogQuery) {
        List<OperateLogExportModel> operateLogExportModelVOS = operateLogRepository.listOperateLog(operateLogQuery);
        Set<DepVo> depVos = new HashSet<>();
        for (OperateLogExportModel vo : operateLogExportModelVOS) {
            if (StringUtils.isNotBlank(vo.getOperateDept()) && StringUtils.isNotBlank(vo.getOperateDeptName())) {
                DepVo depVo = new DepVo();
                depVo.setOwnerDept(vo.getOperateDept());
                depVo.setOwnerDeptName(vo.getOperateDeptName());
                depVos.add(depVo);
            }
        }
        Iterator<DepVo> iterator = depVos.iterator();
        while (iterator.hasNext()) {
            DepVo depVo = iterator.next();
            if (StringUtils.isEmpty(depVo.getOwnerDept()) || StringUtils.isEmpty(depVo.getOwnerDeptName())) {
                iterator.remove();
            }
        }
        return new ArrayList<>(depVos);
    }

    @Override
    public List<OperateResourceTypeVO> operateLogListResourceType(OperateLogQuery operateLogQuery) {
        List<OperateLogExportModel> operateLogExportModelVOS = operateLogRepository.listOperateLog(operateLogQuery);
        List<OperateResourceTypeVO> ortVos = new ArrayList<>();
        for (OperateLogExportModel vo : operateLogExportModelVOS) {
            OperateResourceTypeVO ortVo = new OperateResourceTypeVO();
            ortVo.setOperateResourceType(vo.getOperateResourceType());
            ortVos.add(ortVo);
        }
        Iterator<OperateResourceTypeVO> iterator = ortVos.iterator();
        while (iterator.hasNext()) {
            OperateResourceTypeVO operateResourceTypeVO = iterator.next();
            if (operateResourceTypeVO.getOperateResourceType() != null) {
                iterator.remove();
            }
        }
        Set<OperateResourceTypeVO> set = new HashSet();
        if (!CollectionUtils.isEmpty(ortVos)) {
            for (OperateResourceTypeVO depVo : ortVos) {
                set.add(depVo);
            }
        }
        if (!CollectionUtils.isEmpty(set)) {
            for (OperateResourceTypeVO operateResourceTypeVO : ortVos) {
                switch (operateResourceTypeVO.getOperateResourceType()) {
                    // 操作类目转换中文
                    case 1:
                        operateResourceTypeVO.setOperateResourceTypeName("数据源");
                        break;
                    case 2:
                        operateResourceTypeVO.setOperateResourceTypeName("场景");
                        break;
                    case 3:
                        operateResourceTypeVO.setOperateResourceTypeName("计划");
                        break;
                    case 4:
                        operateResourceTypeVO.setOperateResourceTypeName("消息");
                        break;
                    case 5:
                        operateResourceTypeVO.setOperateResourceTypeName("消息源");
                        break;
                    case 6:
                        operateResourceTypeVO.setOperateResourceTypeName("敏感日志");
                        break;
                    case 7:
                        operateResourceTypeVO.setOperateResourceTypeName("操作日志");
                        break;
                    case 8:
                        operateResourceTypeVO.setOperateResourceTypeName("待办事项");
                        break;
                    default:
                        break;
                }
            }
        }
        return new ArrayList<>(set);
    }

    @Override
    public int insertOperateLog(OperateLog operateLog){
        return operateLogRepository.insertOperateLog(operateLog);
    }

    /**
     * 统计操作日志量
     *
     * @param operateLogQuery
     * @return
     */
    @Override
    public int countOperateLog(OperateLogQuery operateLogQuery) {
        return operateLogRepository.countOperateLog(operateLogQuery);
    }
}
